import 'package:beng_beng_utils/utils/log/BBLogType.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';

import 'SaveToFileUtils.dart';

class BBLogUtils {
  BBLogType showLevel = BBLogType.error;

  // var logger = Logger(
  //     printer: PrettyPrinter(
  //         colors: false, noBoxingByDefault: true, printTime: true));
  bool isCacher = true;
  bool isFireBase = false;
  String appName = "";
  static BBLogUtils? _instance;
  List<BBLogBean> _logList = [];

  List<BBLogBean> get logList => _logList.reversed.toList();

  //单例模式
  static BBLogUtils getInstance() {
    if (_instance == null) {
      _instance = BBLogUtils();
    }
    return _instance!;
  }

// 设置日志级别
  static void setLogLevel(BBLogType level) {
    getInstance().showLevel = level;
  }

  static void setCacher(bool isCacher) {
    getInstance().isCacher = isCacher;
  }

  static void setFireBase(bool isFireBase) {
    getInstance().isFireBase = isFireBase;
  }

  static void setAppName(String appName) {
    getInstance().appName = appName;
  }

  static void clearLog() {
    getInstance()._logList.clear();
  }

  static void d(Object? msg,
      {String? tag = "bbLog d",
      BBLogFrom from = BBLogFrom.people,
      bool isSave = false}) {
    // if (getInstance().showLevel.level <= BBLogType.debug.level) {
    getInstance().printLog(tag, msg, BBLogType.debug, from, isSave: isSave);
    // }
  }

  static void i(Object? msg,
      {String? tag = "bbLog i",
      BBLogFrom from = BBLogFrom.people,
      bool isSave = false}) {
    // if (getInstance().showLevel.level <= BBLogType.info.level) {
    getInstance().printLog(tag, msg, BBLogType.info, from, isSave: isSave);
    // }
  }

  static void w(Object? msg,
      {String? tag = "bbLog w",
      BBLogFrom from = BBLogFrom.people,
      bool isSave = false}) {
    // if (getInstance().showLevel.level <= BBLogType.warning.level) {
    getInstance().printLog(tag, msg, BBLogType.warning, from, isSave: isSave);
  }

  // }

  static void e(Object? msg,
      {String? tag = "bbLog e",
      BBLogFrom from = BBLogFrom.people,
      bool isSave = false}) {
    // if (getInstance().showLevel.level <= BBLogType.error.level) {
    getInstance().printLog(tag, msg, BBLogType.error, from, isSave: isSave);
    // }
  }

  void printLog(String? tag, Object? msg, BBLogType type, BBLogFrom from,
      {bool isSave = false}) {
    var dateTime = DateTime.now();
    if (isFireBase) {
      FirebaseCrashlytics.instance.log(msg.toString());
      FirebaseCrashlytics.instance.log(tag.toString());
      FirebaseCrashlytics.instance.log(type.toString());
      FirebaseCrashlytics.instance.log(from.toString());
    }
    if (isCacher) {
      _logList.add(BBLogBean(
          tag: tag, msg: msg, type: type, from: from, time: dateTime));
    }
    if (isSave || getInstance().showLevel.level <= type.level) {
      appendStringToFile(
          "╔═══════════════════════════════════════════════════════════════════════════════════════════════════" +
              "\n");
      appendStringToFile(msg.toString() + "\n");
      appendStringToFile(tag.toString() + "\n");
      appendStringToFile(type.toString() + "\n");
      appendStringToFile(from.toString() + "\n");
      appendStringToFile(dateTime.toString() + "\n");
      appendStringToFile(
          "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" +
              "\n");
    }

    if (getInstance().showLevel.level <= type.level) {
      if (type != BBLogType.none) {
        print("BBLogUtils tag:$tag from:$from time:${dateTime}");
      }
      print(
          "╔═══════════════════════════════════════════════════════════════════════════════════════════════════");

      switch (type) {
        case BBLogType.debug:
          print(msg);
          break;
        case BBLogType.info:
          print(msg);
          break;
        case BBLogType.warning:
          print(msg);
          break;
        case BBLogType.error:
          print(msg);
          break;
        case BBLogType.none: //不打印日志
          break;
      }
      print(
          "╚═══════════════════════════════════════════════════════════════════════════════════════════════════");
    }
  }
}
